/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package MyClass;

import java.util.Random;
import javax.microedition.lcdui.Image;

/**
 *
 * @author HuuTai_it
 */
public class GhostShip extends Ship {
    
    
    public GhostShip (GameManagement gameMng, Image image) throws Exception {
        
        super(gameMng, image);
        boolean temp = true;
        while(temp) {
            
            int x = Math.abs(gameMng.r.nextInt() % 10);
            int y = Math.abs(gameMng.r.nextInt() % 10);
            int mX = gameMng.mainShip.getX() / 24;
            int mY = gameMng.mainShip.getY() / 24;
            
            if(gameMng.element[x][y] == 0 && (Math.abs(x - mX) > 3 || Math.abs(y - mY) > 3)) {
                
                setPosition(x * 24, y * 24);
                gameMng.element[x][y] = 8;
                temp = false;
            }
        }
        
       sType = 5;
    }    
    public int autoMove(Ship s){
        try
        {
       
        if(Math.abs(s.getX() - getX()) <= 24 && Math.abs(s.getY() - getY()) <= 24)
        {//kill
            return super.bAutoMove(s, 6);
        }
        int dir[] = {0, 0, 0};
        if(!startMove)
        {
            if(getX() < 24 && ( direction == 6 ||direction == 7|| direction == 8) ) 
                direction = 3;
            else if(getX() >= 216 && ( direction == 2 ||direction == 3|| direction == 4) ) 
                direction = 7;
            if(getY() < 24 && ( direction == 1 ||direction == 2|| direction == 8) ) 
                direction = 5;
            else if(getY() >= 216 && ( direction == 4 ||direction == 5|| direction == 6) ) 
                direction = 1;
            
        }
        
        //start
        if( !startMove && count == 0)
        {
            //edit direction khi 
            if(getX() < 24 && ( direction == 6 ||direction == 7|| direction == 8) ) 
                direction = 3;
            else if(getX() >= 216 && ( direction == 2 ||direction == 3|| direction == 4) ) 
                direction = 7;
            if(getY() < 24 && ( direction == 1 ||direction == 2|| direction == 8) ) 
                direction = 5;
            else if(getY() >= 216 && ( direction == 4 ||direction == 5|| direction == 6) ) 
                direction = 1;
            boolean f = true;//la`n da`u
            
            int randir  = 0;
            while(randir < 3 &&
                    ((gameMng.element[getX()/24 + dirc[0][direction - 1]/24]
                    [getY()/24+ dirc[1][direction - 1]/24] != 0 ) || f)
                    && (dir[0]* dir[1] * dir[2] == 0))
            {
                
                f = false;
                do
                {
                //di 1 trong 3 huong
                    randir++;
                }
                while (randir < 3 && dir[randir] > 0  && (dir[0]* dir[1] * dir[2] == 0));
                if(randir < 3) dir[randir]++;
                //trong 3 huong da chon khong di duoc
                if((dir[0] > 1 && dir[1] > 1 && dir[2] > 1))
                    break;
                //lay huong
                if(getX() < s.getX() ) 
                {
                        if(getY() < s.getY())
                        {
                            //main  dong nam
                            if(s.direction % 2 != 0)
                                switch(randir)
                                    {
                                        case 0: direction = 5; break;
                                        case 1: direction = 3; break;
                                        case 2: direction = 4; break;
                                    }
                            else
                                switch(randir)
                                    {
                                        case 0: direction = 4; break;
                                        case 1: direction = 3; break;
                                        case 2: direction = 5; break;
                                    }
                        }
                        else if(getY() > s.getY())
                        {
                            //main dong bac
                            if(s.direction % 2 != 0)
                                switch(randir)
                                    {
                                        case 0: direction = 3; break;
                                        case 1: direction = 1; break;
                                        case 2: direction = 2; break;
                                    }
                            else
                                switch(randir)
                                    {
                                        case 0: direction = 2; break;
                                        case 1: direction = 1; break;
                                        case 2: direction = 3; break;
                                    }
                        }
                        else
                            //main dong
                            if(s.direction % 2 == 0)
                                switch(randir)
                                    {
                                        case 0: direction = 3; break;
                                        case 1: direction = 2; break;
                                        case 2: direction = 4; break;
                                    }
                            else
                                switch(randir)
                                    {
                                        case 0: direction = 4; break;
                                        case 1: direction = 2; break;
                                        case 2: direction = 3; break;
                                    }
                    }else
                        if(getX() > s.getX())
                        {
                            if(getY() < s.getY())
                            {
                                //main tay nam
                                if(s.direction % 2 != 0)
                                switch(randir)
                                    {
                                        case 0: direction = 5; break;
                                        case 1: direction = 7; break;
                                        case 2: direction = 6; break;
                                    }
                            else
                                switch(randir)
                                    {
                                        case 0: direction = 6; break;
                                        case 1: direction = 7; break;
                                        case 2: direction = 5; break;
                                    }
                            }
                            else if(getY() > s.getY())
                            {    
                                //main tay bac
                                if(s.direction % 2 != 0)
                                switch(randir)
                                    {
                                        case 0: direction = 1; break;
                                        case 1: direction = 7; break;
                                        case 2: direction = 8; break;
                                    }
                            else
                                switch(randir)
                                    {
                                        case 0: direction = 8; break;
                                        case 1: direction = 7; break;
                                        case 2: direction = 1; break;
                                    }
                            }
                            else
                                //main tay
                                if(s.direction % 2 == 0)
                                switch(randir)
                                    {
                                        case 0: direction = 7; break;
                                        case 1: direction = 8; break;
                                        case 2: direction = 6; break;
                                    }
                            else
                                switch(randir)
                                    {
                                        case 0: direction = 6; break;
                                        case 1: direction = 8; break;
                                        case 2: direction = 7; break;
                                    }                                      
                        }
                    else
                       if(getY() < s.getY())
                        {
                            //nam 
                            if(s.direction % 2 == 0)
                                switch(randir)
                                    {
                                        case 0: direction = 5; break;
                                        case 1: direction = 6; break;
                                        case 2: direction = 4; break;
                                    }
                            else
                                switch(randir)
                                    {
                                        case 0: direction = 4; break;
                                        case 1: direction = 6; break;
                                        case 2: direction = 5; break;
                                    }
                        }
                        else if(getY() > s.getY())
                        {
                            //bac
                            if(s.direction % 2 == 0)
                                switch(randir)
                                    {
                                        case 0: direction = 1; break;
                                        case 1: direction = 2; break;
                                        case 2: direction = 8; break;
                                    }
                            else
                                switch(randir)
                                    {
                                        case 0: direction = 8; break;
                                        case 1: direction = 2; break;
                                        case 2: direction = 1; break;
                                    }
                        }
                    if(getX() < 24 && ( direction == 6 ||direction == 7|| direction == 8) ) 
                    direction = 3;
                    else if(getX() >= 216 && ( direction == 2 ||direction == 3|| direction == 4) ) direction = 7;
                    if(getY() < 24 && ( direction == 1 ||direction == 2|| direction == 8) ) 
                        direction = 5;
                    else if(getY() >= 216 && ( direction == 4 ||direction == 5|| direction == 6) ) direction = 1;
            }
            
            //3 huong bi chan => di random nhung k dc dam vao cac doi tuong khac
            int[] arr = {0, 0, 0, 0, 0, 0, 0, 0};
            if(dir[0] == 1 && dir[1] == 1 && dir[2] == 1)
                while((gameMng.element[getX()/24 + dirc[0][direction - 1]/24]
                        [getY()/24 + dirc[0][direction - 1]/24] != 0) &&
                        arr[0]*arr[1]*arr[2]*arr[3]*arr[4]*arr[5]*arr[6]*arr[7] == 0)
                {              
                    do
                    {
                        direction =  Math.abs(gameMng.r.nextInt() % 8) + 1;
                    }
                    while(arr[direction - 1] > 0  && (arr[0]*arr[1]*arr[2]*arr[3]*arr[4]*arr[5]*arr[6]*arr[7] == 0));
                    arr[direction - 1] ++;
                    
                    if(getX() < 24) 
                    direction = 3;
                    else if(getX() >= 216) direction = 7;
                     if(getY() < 24) 
                        direction = 5;
                    else if(getY() >= 216) direction = 1;
                }   
            if(arr[0]>1 && arr[1]>1 && arr[2]>1 && arr[3]>1 && arr[4]>1 
                    && arr[5]>1 && arr[6]>1 && arr[7]> 1  )
            { 
                for(int i = 0; i < 8; i++) arr[i] = 0;
                while((gameMng.element[getX()/24 + dirc[0][direction - 1]/24]
                        [getY()/24+ dirc[0][direction - 1]/24] < 3 ) &&
                        arr[0]*arr[1]*arr[2]*arr[3]*arr[4]*arr[5]*arr[6]*arr[7] == 0)
                {
                    direction =  Math.abs(gameMng.r.nextInt() % 8)  + 1;
                    arr[direction - 1] = 1;
                    if(getX() < 24) 
                    direction = 3;
                    else if(getX() >= 216) direction = 7;
                    if(getY() < 24) 
                        direction = 5;
                    else if(getY() >= 216) direction = 1;
                }
            }
            //tim dc huong di
            
            gameMng.element[getX()/24][getY()/24] = 0;
            gameMng.element[(getX() + dirc[0][direction - 1])/24]
                    [(getY()+ dirc[1][direction - 1])/24] = 6;
            startMove = true;
            count ++;
            xDes = getX() + dirc[0][direction - 1];
            yDes = getY() + dirc[1][direction - 1];
            return 0;
        }
        if(getX() == xDes && getY() == yDes)
        {
            startMove = false;   
            count =0;
            return 1;//endmove
        }
        if(startMove)       
        {            
            move(dirc[0][direction - 1]/12, dirc[1][direction - 1]/12);
            switch(direction) {            
            case 1:setFrameSequence(frameUp);break;
            case 2:setFrameSequence(frameUpRight);break;
            case 3:setFrameSequence(frameRight);break;
            case 4:setFrameSequence(frameDownRight);break;
            case 5:setFrameSequence(frameDown);break;
            case 6:setFrameSequence(frameDownLeft);break;
            case 7:setFrameSequence(frameLeft);break;
            case 8:setFrameSequence(frameUpLeft);break;
            } 
        }
        return 2;
        }
        catch(ArrayIndexOutOfBoundsException ex)
        {
            ex.printStackTrace();
            System.out.println(ex + " " +getX()/24 + "x" +getY()/24 + ":" + direction);
        }
        return 0;
    }
    public int fire(){
        return 0;
    }
}
